//
//  SEGYellowPageViewModel.m
//  Commom
//
//  Created by 乔杨 on 2020/6/2.
//

#import "SEGYellowPageViewModel.h"
 
@implementation SEGYellowPageViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
      [self createSignal];
    }
    return self;
}
- (void)createSignal {
    @weakify(self);
    self.signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        //Id匹配请求
        [self.yellowPagesManager requestYellowPagesIdsAndList:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
            if (isSuccessed) {
                self.allArray = self.yellowPagesManager.dataSourceAllCatalog;
                self.normalArray = self.yellowPagesManager.dataSourceNormal;
            }else {
                [subscriber sendNext:@(0)];
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:codeType.intValue userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                return;
            }
            [subscriber sendNext:@(self.allArray.count+self.normalArray.count)];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

#pragma mark - Getter
- (YellowPagesManager *)yellowPagesManager {
    if (!_yellowPagesManager) {
        _yellowPagesManager = [[YellowPagesManager alloc] init];
    }
    return _yellowPagesManager;
}

@end
